var ds;
function displayTypePage() {
	Ext.form.Field.prototype.msgTarget = 'side';
	Ext.QuickTips.init();

	var sm = new Ext.grid.RowSelectionModel({
		singleSelect : true
	});
	ds = new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : DISPATCH_URL
		}),
		reader : new Ext.data.JsonReader({
			root : 'types',
			id : 'id',
			fields : [ {
				name : 'id'
			}, {
				name : 'name'
			}, {
				name : 'description'
			} ]
		})
	});
	var gridCm = new Ext.grid.ColumnModel([ new Ext.grid.RowNumberer(), {
		header : TXT_type_name,
		dataIndex : 'name',
		width : 200
	}, {
		header : TXT_type_description,
		dataIndex : 'description',
		width : 200
	} ]);
	var btnCreate = new Ext.Button({
		text : TXT_common_create,
		tooltip : TXT_common_create,
		iconCls : 'icoAdd'
	});

	var btnEdit = new Ext.Button({
		text : TXT_common_edit,
		tooltip : TXT_common_edit,
		iconCls : 'icoDetail'
	});

	var btnDelete = new Ext.Button({
		text : TXT_common_delete,
		tooltip : TXT_common_delete,
		iconCls : 'icoDel'
	});
	btnCreate.on('click', function() {
		createTypeForm('', TXT_common_create);
	});
	btnEdit.on('click', function() {
		if (sm.getCount() == 0) {
			Ext.MessageBox.alert(TXT_common_hint, TXT_user_need);
			return;
		}
		var record = sm.getSelected();
		createTypeForm(record.id, TXT_common_edit);
	});
	btnDelete.on('click', function() {
		if (sm.getCount() == 0) {
			return;
		} else {
			Ext.MessageBox.alert(TXT_common_hint, "不能删除类型数据");
		}
	});
	var grid = new Ext.grid.GridPanel({
		el : 'grid',
		ds : ds,
		cm : gridCm,
		sm : sm,
		stripeRows : true,
		loadMask : true,
		title : TXT_type_title,
		tbar : [ btnCreate, '-', btnEdit, '-', btnDelete ]
	});
	ds.baseParams = {
		cmd : 'type',
		action : 'find'
	};
	ds.on('load', function() {
		grid.getSelectionModel().selectFirstRow();
	});
	ds.load();

	BestvSearch.glb_mainPanel.add(grid);
	BestvSearch.drawMainFrame(TXT_app_name + ' - ' + TXT_type_title);
}

function reloadTypes() {
	ds.reload();
}

Ext.onReady(displayTypePage);

function createTypeForm(typeId, s_title) {
	var tid = typeId;
	var typeForm = new Ext.FormPanel({
		labelAlign : 'right',
		labelWidth : 150,
		height : 130,
		width : 380,
		margins : '0 0 2 0',
		cmargins : '0 0 2 0',
		frame : true,
		defaults : {
			width : 180
		},
		defaultType : 'textfield',
		items : [ {
			fieldLabel : TXT_type_name,
			id : 'typeName',
			name : 'typeName',
			allowBlank : false
		}, {
			fieldLabel : TXT_type_description,
			id : 'typeDescription',
			name : 'typeDescription',
			allowBlank : false
		} ]
	});
	var win = null;
	if (!win) {
		win = new Ext.Window(
				{
					title : s_title,
					height : 130,
					width : 380,
					autoScroll : false,
					layout : 'form',
					border : false,
					minimizable : false,
					maximizable : false,
					resizable : false,
					modal : true,
					layoutConfig : {
						animate : false
					},
					items : typeForm,
					buttonAlign : 'center',
					buttons : [
							{
								text : TXT_common_OK,
								handler : function() {
									if (!typeForm.form.isValid())
										return;
									var typeName = typeForm
											.findById('typeName').getValue();
									var typeDescription = typeForm.findById(
											'typeDescription').getValue();
									if (typeName == '') {
										Ext.MessageBox.alert(TXT_common_hint,
												TXT_type_name_not_null);
										return;
									}
									BestvSearch.showWaitingWin();
									Ext.Ajax
											.request({
												url : DISPATCH_URL,
												mehtod : 'post',
												params : 'cmd=type&action=save&id='
														+ tid
														+ '&typeName='
														+ typeName
														+ '&typeDescription='
														+ typeDescription,
												success : function(response,
														options) {
													Ext.MessageBox.hide();
													var s = response.responseText;
													if (s == 'error') {
														Ext.MessageBox
																.alert(
																		TXT_common_hint,
																		TXT_common_access_fail);
													} else if (s == 'null') {
														Ext.MessageBox
																.alert(
																		TXT_common_hint,
																		TXT_common_relogin,
																		function() {
																			window.location.href = '../eni/common/logout.jsp';
																		});
													} else {
														var result = Ext.util.JSON
																.decode(s);
														if (result.result == 'success') {
															if (result.message == 'new')
																Ext.MessageBox
																		.alert(
																				TXT_common_hint,
																				TXT_site_create_success,
																				function() {
																					win
																							.close();
																				});
															else if (result.message == 'edit')
																Ext.MessageBox
																		.alert(
																				TXT_common_hint,
																				TXT_edit_success,
																				function() {
																					win
																							.close();
																				});
															win.close();
															reloadTypes();
														} else if (result.message == 'name') {
															Ext.MessageBox
																	.alert(
																			TXT_common_hint,
																			TXT_type_name_exist);
														}
													}
												},
												failure : function(respond,
														options) {
													Ext.MessageBox.hide();
													Ext.MessageBox
															.alert(
																	TXT_common_hint,
																	TXT_common_access_fail);
												}
											});
								}
							}, {
								text : TXT_common_cancel,
								handler : function() {
									win.close();
								}
							} ]
				});
	}
	win.on('show', function() {
		if (tid != '') {
			Ext.Ajax.request({
				url : DISPATCH_URL,
				method : 'post',
				params : 'cmd=type&action=get&id=' + tid,
				success : function(response, options) {
					var s = response.responseText;
					if (s == '[error]')
						Ext.MessageBox.alert(TXT_common_hint,
								TXT_common_access_fail);
					else if (s == '[null]') {
						Ext.MessageBox.alert(TXT_common_hint,
								TXT_common_relogin, function() {
									window.location.href = LOGIN_URL;
								});
					} else {
						var result = Ext.util.JSON.decode(s);
						typeForm.findById('typeName').setValue(result.name);
						typeForm.findById('typeDescription').setValue(
								result.description);
					}
				},
				failure : function(response, options) {
					Ext.MessageBox.alert(TXT_common_hint,
							TXT_common_access_fail);
				}
			});
		}
	});
	win.show();
}